package com.test.io.utils;

/**
 * describe: 字节数组工具
 *
 * @author lianying
 * @date 2019/03/04
 */
public class ByteArrayUtils {
    /**
     * 判断两个byte数组是否相等
     *
     * @param arg1
     * @param arg2
     * @return
     */
    public static boolean equal(byte[] arg1, byte[] arg2) {
        if (arg1.length != arg2.length) {
            return false;
        }
        for (int i = 0; i < arg1.length; i++) {
            if (arg1[i] != arg2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 整数转换成长度4的字节数组
     *
     * @return
     */
    public static byte[] int2ByteArray(int arg) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (0xff & (arg >> 24));
        bytes[1] = (byte) (0xff & (arg >> 16));
        bytes[2] = (byte) (0xff & (arg >> 8));
        bytes[3] = (byte) (arg & 0xff);
        return bytes;
    }

    /**
     * 长度4的字节数组转换成整数
     *
     * @return
     */
    public static int byteArray2Int(byte[] bytes) {
        int sum;
        sum = ((bytes[0] & 0xff) << 24);
        sum = sum | ((bytes[1] & 0xff) << 16);
        sum = sum | ((bytes[2] & 0xff) << 8);
        sum = sum | ((bytes[3] & 0xff));
        return sum;
    }
}
